<?php

/**
 * 获取指定目录下的文件和目录列表
 *
 * @param string $directory 要扫描的目录
 * @return system\library\DirScanner
 */
// function scan_dir(string $directory): system\library\DirScanner
// {
//     return new system\library\DirScanner($directory);
// }

function halt($var)
{
    var_dump($var);
    exit;
}


function scan_files(string $dir, $suffix = 'php'): array
{
    $files = array();
    $scanned_directory = array_diff(scandir($dir), array('..', '.'));
    foreach ($scanned_directory as $file) {
        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (is_dir($path)) {
            // 递归调用函数获取子目录下的文件
            $files = array_merge($files, scan_files($path));
        } else {
            $extension = pathinfo($path)['extension'];
            if ($extension == $suffix) {
                // 添加文件路径到数组中
                $files[] = $path;
            }
        }
    }
    return $files;
}


function output($text, $color = null, $newLine = true)
{
    $styles = [
        'success' => "\033[0;32m%s\033[0m",
        'error' => "\033[31;31m%s\033[0m",
        'info' => "\033[33;33m%s\033[0m"
    ];

    $format = '%s';
    if (isset($styles[$color])) {
        $format = $styles[$color];
    }

    if ($newLine) {
        $format .= PHP_EOL;
    }

    printf($format, $text);
}
